<?php

namespace App\Models;

use App\Http\Controllers\AccessSystem\CommonController;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * 数字资源推荐阅读书籍表
 */
class ResourceReadRecomBook extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_read_recom_book';




    /**
     * 获取书单书籍列表
     * @param limit int 分页大小
     * @param read_id 推荐阅读id
     * @param big_type_id 系统大类
     * @param keywords string 搜索关键词
     */
    public function lists($search_data = [])
    {
        $read_id = $search_data['read_id'] ?? '';
        $field = $search_data['field'] ?? '';
        $big_type_id = $search_data['big_type_id'] ?? '';
        $keywords = $search_data['keywords'] ?? '';
        $limit = !empty($search_data['limit']) ? $search_data['limit'] : 10;
        //获取书单数据
        $resourceReadRecomModel = new ResourceReadRecom();
        $read_info = $resourceReadRecomModel->detail($read_id);
        if (empty($read_info)) {
            return [];
        }

        $resourceTotalModel = new ResourceTotal();
        if (empty($field)) {
            $field = ['r.id', 'r.read_id', 'r.resource_id', 'r.create_time', 'resource_business_id', 't.big_type_id', 'type_id', 'guid', 'title', 'author', 'press', 'pre_time', 'isbn', 'img', 'intro', 't.browse_num'];
        }

        //获取敏感词
        // $sensitiveWordModel = new SensitiveWord();
        // $sensitive_word = $sensitiveWordModel->getSensitiveWordALL();
        $res = $this->from($this->getTable() . " as r")->select($field)
            ->join($resourceTotalModel->getTable() . ' as t', 't.id', '=', 'r.resource_id')
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('title', 'like', "%$keywords%")->Orwhere('author', 'like', "%$keywords%");
                }
            })->where(function ($query) use ($big_type_id) {
                if ($big_type_id) {
                    $query->where('t.big_type_id', $big_type_id);
                }
            })
            // ->where(function ($query) use ($sensitive_word) {
            //     if ($sensitive_word) {
            //         foreach ($sensitive_word as $item) {
            //             $query->where('title', 'not like', "%$item%");
            //         }
            //     }
            // })->where(function ($query) use ($sensitive_word) {
            //     if ($sensitive_word) {
            //         foreach ($sensitive_word as $item) {
            //             $query->where('author', 'not like', "%$item%");
            //         }
            //         $query->orWhereNull('author');
            //     }
            // })
            ->where('is_mask', 1)
            ->where('read_id', $read_id)
            ->where('t.is_del', 1)
            ->orderByDesc('r.id')
            ->paginate($limit)
            ->toArray();

        foreach ($res['data'] as $key => $val) {
            $res['data'][$key]['resource_business_name'] = !empty($val['resource_business_id']) ? ResourceBusinessName::getBusinessName($val['resource_business_id']) : '';
            $res['data'][$key]['type_name'] = !empty($val['type_id']) ? ResourceTypeExternal::where('id', $val['type_id'])->where('is_del', 1)->value('type_name') : '';
            $res['data'][$key]['browse_num'] = !empty($val['browse_num']) ? format_number($val['browse_num']) : null; //位数转换
        }

        return $res;
    }


    /**
     * 添加系统类型作品
     * @param read_id int 推荐阅读id
     * @param resource_ids int 系统类型作品的id 多个逗号拼接
     */
    public function addResourceReadRecomBook($read_id, $resource_ids)
    {
        $old_resource_ids = $this->where('read_id', $read_id)->pluck('resource_id')->toArray();
        $resource_ids = explode(',', $resource_ids);
        foreach ($resource_ids as $key => $val) {
            if (!in_array($val, $old_resource_ids)) {
                $obj = new self();
                $obj->read_id = $read_id;
                $obj->resource_id = $val;
                $obj->save();
            }
        }
        return true;
    }


    /**
     * 删除系统类型作品
     * @param read_id int 推荐阅读id
     * @param read_recom_resource_ids  数据id 多个逗号拼接
     */
    public function delResourceReadRecomBook($read_id, $read_recom_resource_ids)
    {
        $read_recom_resource_ids = explode(',', $read_recom_resource_ids);
        return $this->where('read_id', $read_id)->whereIn('id', $read_recom_resource_ids)->delete();
    }
    /**
     * 获取系统类型作品
     * @param read_id int 推荐阅读id
     */
    public function getResourceReadRecomBookId($read_id, $big_type_id = null)
    {
        return $this->from($this->getTable() . ' as b')
            ->join('resource_total as t', 't.id', '=', 'b.resource_id')
            ->where('read_id', $read_id)
            ->where(function ($query) use ($big_type_id) {
                if ($big_type_id) {
                    $query->where('t.big_type_id', $big_type_id);
                }
            })->pluck('resource_id')
            ->toArray();
    }
}
